package top.xylweb.attend.service;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.xylweb.attend.dao.AttendMapper;
import top.xylweb.attend.entity.Attend;
import top.xylweb.attend.vo.QueryCondition;
import top.xylweb.common.page.PageQueryBean;
import top.xylweb.common.utils.DateUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("attendServiceImpl")
public class AttendServiceImpl implements AttendServiceInter {

    /**
     * 中午十二点 判定上下午
     */
    private static final int NOON_HOUR = 12 ;
    private static final int NOON_MINUTE = 00 ;

    /**
     * 早晚上班时间判定
     */
    private static final int MORNING_HOUR = 9;
    private static final int MORNING_MINUTE = 30;
    private static final int EVENING_HOUR = 18;
    private static final int EVENING_MINUTE = 30;

    /**
     * 缺勤一整天
     */
    private static final Integer ABSENCE_DAY =480 ;
    /**
     * 考勤异常状态
     */
    private static final Byte ATTEND_STATUS_ABNORMAL = 2;
    private static final Byte ATTEND_STATUS_NORMAL = 1;

    Log log = LogFactory.getLog(AttendServiceImpl.class);

    @Autowired
    private AttendMapper attendMapper;

    /**
     * @Author X [**.**.**]
     * @Date 2017/12/18 19:15
     * Description  模拟添加  签到
     **/
    @Override
    public void signAttend(Attend attend) throws Exception {
        try {
            Date today = new Date();
            attend.setAttendDate(today);                            //当前时间
            attend.setAttendWeek((byte)DateUtils.getWeek());        //获取 第几周

            Attend attendRecord = attendMapper.selectTodaySignRecord(attend.getUserId());   //查询打卡时  是否有打卡记录

            Date none = DateUtils.getDate(NOON_HOUR,NOON_MINUTE);   //上午时间
            Date morningAttend = DateUtils.getDate(MORNING_HOUR,MORNING_MINUTE);//九点半之前
            if(attendRecord==null){     //判断  打卡时 是否 当天之前有打卡记录  为空时 并无打卡记录 则进行打卡
                if(today.compareTo(none)<=0){      //compare 比较两个日期  如果today(当前时间) 小于none(中午12点) 的时间 返回负数
                    //打卡时间  早于十二点  上午打卡
                    attend.setAttendMorning(today);
                    //计算打卡时间是否迟到
                    if(today.compareTo(morningAttend)>0){
                        //大于九点半迟到了
                        attend.setAttendStatus(ATTEND_STATUS_ABNORMAL);
                        attend.setAbsence(DateUtils.getMinute(morningAttend,today));
                    }
                }else{
                    //晚上打卡时间
                    attend.setAttendEvening(today);
                }
                attendMapper.insertSelective(attend);
            }else{
                if(today.compareTo(none)<=0){      //compare 比较两个日期  如果today(当前时间) 小于none(中午12点) 的时间 返回负数
                    //打卡时间  早于十二点  上午打卡
                    return;
                }else{
                    //晚上打卡
                    attendRecord.setAttendEvening(today);
                    //判断打卡时间是不是18.30以后是不是早退
                    Date eveningAttend = DateUtils.getDate(EVENING_HOUR,EVENING_MINUTE);
                    if(today.compareTo(eveningAttend)<0){
                        //早于下午六点半 早退
                        attendRecord.setAttendStatus(ATTEND_STATUS_ABNORMAL);
                        attendRecord.setAbsence(DateUtils.getMinute(today,eveningAttend));
                    }else {
                        attendRecord.setAttendStatus(ATTEND_STATUS_NORMAL);
                        attendRecord.setAbsence(0);
                    }
                    attendMapper.updateByPrimaryKeySelective(attendRecord);
                }
            }
        }catch (Exception e){
            log.error("签到失败异常："+e);
            throw e;
        }
    }

    /**
     * @Author X [**.**.**]
     * @Date 2018/1/8 16:45
     * Description  和ajax相互结合  实现前端签到 分页
     **/
    @Override
    public PageQueryBean listAttend(QueryCondition condition) {
        //先根据条件 进行查询   如果查询到的 数据为null 就不进行显示
        int count = attendMapper.countByCondition(condition);
        PageQueryBean pageResult = new PageQueryBean();
        if(count>0){
            pageResult.setTotalRows(count);
            pageResult.setCurrentPage(condition.getCurrentPage());
            pageResult.setPageSize(condition.getPageSize());
            List<Attend> list = attendMapper.selectAttendPage(condition);
            pageResult.setItems(list);
        }
        return pageResult;
    }

    @Override
    public void checkAttend() {
        //查询缺勤用户ID 插入打卡记录  并且设置为异常 缺勤480分钟
        List<Long> userIdList =attendMapper.selectTodayAbsence();
        if(CollectionUtils.isNotEmpty(userIdList)){
            List<Attend> attendList = new ArrayList<Attend>();
            for(Long userId:userIdList){
                Attend attend = new Attend();
                attend.setUserId(userId);
                attend.setAttendDate(new Date());
                attend.setAttendWeek((byte)DateUtils.getWeek());
                attend.setAbsence(ABSENCE_DAY);
                attend.setAttendStatus(ATTEND_STATUS_ABNORMAL);
                attendList.add(attend);
            }
            attendMapper.batchInsert(attendList);
        }
        // 检查晚打卡 将下班未打卡记录设置为异常
        List<Attend> absenceList = attendMapper.selectTodayEveningAbsence();
        if(CollectionUtils.isNotEmpty(absenceList)){
            for(Attend attend : absenceList){
                attend.setAbsence(ABSENCE_DAY);
                attend.setAttendStatus(ATTEND_STATUS_ABNORMAL);
                attendMapper.updateByPrimaryKeySelective(attend);
            }
        }
    }

}
